/**
* @Author: qinhaoda
* @Description:
* @File: label
* @Version: 1.0.0
* @Date: 2022/12/30 14:35
 */
package timer

import (
	"github.com/didi/nightingale/src/modules/agent/config"
	"github.com/didi/nightingale/src/modules/agent/ecache"
	"github.com/didi/nightingale/src/modules/agent/report"
	"github.com/didi/nightingale/src/modules/agent/stra"
	"github.com/toolkits/pkg/logger"
	"math/rand"
	"time"
)

func UploadLabel() {
	uploadLabel()
	interval := time.Duration(3600) * time.Second
	for {
		uploadLabel()
		rand.Seed(time.Now().UnixNano())
		d := rand.Intn(10)
		randTime := time.Duration(d) * time.Second
		time.Sleep(interval + randTime)
	}
}

func uploadLabel() {
	labels := ecache.LabelCache.GetAll()
	data := map[string]interface{}{
		"hostname": config.Endpoint,
		"envtype":  "label",
		"node":     stra.Collect.Node,
		"doc":      labels, //这个就是刚才给的environment的里的一部分，比如cron，就上传CronInfo的json; service就是ServiceInfo, "general|personal",就是CheckInfo
	}
	err := report.ReportTransferData(data, true)
	if err != nil {
		logger.Errorf("upload label data error, error=%v", err)
	}
}
